import json

from apps.crawler_platform.core_platform.core_sql import CoreSqlMixin


class EtlValidate(object):
    def __init__(self):
        pass

    @classmethod
    async def base_validate(cls, redis, data):
        status = True
        rsdic = {}
        for k, v in data.items():
            if v == None:
                status = False
                break
        if status:
            sub_db = await redis.hget("sub_db", data.get("sub_db_id", ""))
            # tmps = await CoreSqlMixin.check_sub_sb(data.get("sub_db_id", ""), data.get("sub_db", ""),
            #                                        data.get("product", ""), data.get("provider", ""))
            if sub_db:
                sub_db = json.loads(sub_db)
            error_val = []
            if "filename" not in data.keys():
                if not sub_db or sub_db["sub_db"] != data["sub_db"] or sub_db["product"] != data["product"] or sub_db[
                    "provider"] != data["provider"] or sub_db["source_type"] != data["source_type"]:
                    rsdic = {
                        "tag": "base_validate",
                        "msg": "sub_db验证出错",
                        "error_val": []
                    }
                    return False, rsdic
                if len(data["title"]) == 0:
                    error_val.append("title")
            pub_date = data.get("pub_date", "")
            pub_year = data.get("pub_year", "")
            if ("filename" not in data.keys() and len(pub_date) > 0) or ("filename" in data.keys()):
                if len(pub_year) == 0 or len(pub_year) !=4 or pub_year[0:2] not in ("18","19","20"):
                    error_val.append("pub_year")

            if "keyid" not in data.keys():
                error_val.append("keyid")
            if "is_deprecated" not in data.keys() or data["is_deprecated"] == None or data["is_deprecated"] == "":
                error_val.append("is_deprecated")
            if len(error_val) > 0:
                status = False
        if not status:
            if len(error_val) == 0:
                msg = "/etl_api/validate 基础检测 字段有空值"
            else:
                msg = "/etl_api/validate 基础检测 不合法字段:{}".format(",".join(error_val))
            rsdic = {
                "tag": "base_validate",
                "msg": msg,
                "error_val": error_val
            }
            print(rsdic)
        return status, rsdic

    @classmethod
    def validate_qk(cls, data):
        status = True
        rsdic = {}
        error_val = []
        if data["sub_db_id"] not in ["00393", "00429", "00854", "00855", "00017","00164"]:
            if data.get("num", "") == "":
                error_val.append("num")
        if len(data["title"]) == 0:
            error_val.append("title")
        if len(error_val) > 0:
            status = False
        if not status:
            msg = "/etl_api/validate 期刊规则检测 不合法字段:{}".format(",".join(error_val))
            rsdic = {
                "tag": "special_validate",
                "msg": msg,
                "error_val": error_val
            }
        return status, rsdic

    @classmethod
    def repaireData(cls, data):
        """
        全局修复数据
        :param data:
        :return:
        """
        if "keyid" not in data.keys():
            data["keyid"] = data["lngid"]
        if "is_deprecated" not in data.keys() or data["is_deprecated"] == None or data["is_deprecated"] == "":
            data["is_deprecated"] = "0"
        data = {k: "" if not v else v for k, v in data.items()}
        return data

    @classmethod
    async def validate_data_entry(cls, redis, data):
        """
        验证方法，且兼容修复数据
        :param redis:
        :param data:
        :return:
        """
        # 修复数据
        data = cls.repaireData(data)
        # 通用验证
        bools, rsdic = await cls.base_validate(redis, data)
        if not bools:
            return bools, rsdic
        source_type = data["source_type"]
        # 类型验证
        if source_type == "3" and "filename" not in data.keys():
            bools, rsdic = cls.validate_qk(data)
            if not bools:
                return bools, rsdic
        # 特殊验证
        # 如果需要，在这里写特殊验证
        return True, {}

    @classmethod
    def etl_del(cls, data):
        if "rawid_mysql" in data.keys():
            data.pop("rawid_mysql")
        if "src_db" in data.keys():
            data.pop("src_db")
